package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("role")
public class RoleController {
    @Autowired
    public IRoleService roleService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"role:view", "角色界面"}, logical = Logical.OR)
    public String view() {
        return "role/role";
    }

    @ResponseBody
    @RequestMapping("query")
    public PageResult query(QueryObject qo) {
        return roleService.query(qo);
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<Role> selectAll() {
        return roleService.selectAll();
    }

    @RequestMapping("delete")
    @ResponseBody
    @RequiresPermissions(value = {"role:delete", "角色删除"}, logical = Logical.OR)
    public JSONResult delete(Long id) {
        try {
            roleService.deleteByPrimaryKey(id);
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("删除失败");
        }
    }
    @ResponseBody
    @RequestMapping("saveOrUpdate")
    @RequiresPermissions(value = {"role:saveOrUpdate", "角色新增或编辑"}, logical = Logical.OR)
    public JSONResult saveOrUpdate(Role role){
        try {
            if(role.getId() != null){
                roleService.updateByPrimaryKey(role);
            }else{
                roleService.insert(role);
            }
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("操作失败");
        }
    }
    @ResponseBody
    @RequestMapping("getMenuByRoleId")
    public List<Long> getMenuByRoleId(Long roleId){
        return roleService.getMenuByRoleId(roleId);
    }
}
